package com.banciyuan.bcywebview.biz.main.group.focused;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import java.util.List;

import de.greenrobot.daoexample.model.SimpleTeam;

/**
 * Created by hy on 2016/4/7.
 */
public class FocusedGroupAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<SimpleTeam> data;

    public FocusedGroupAdapter(Context context, List<SimpleTeam> data) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        final SimpleTeam simpleTeam = data.get(position);
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.group_focus_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.groupTitle.setText(Html.fromHtml(simpleTeam.getName()));
        viewHolder.focuseNum.setText(simpleTeam.getMember_num());
        viewHolder.itemsNum.setText(simpleTeam.getPost_num());
        viewHolder.time.setText(StringUtil.getOppositeTime(simpleTeam.getPtime()));
        int newCount = simpleTeam.getNew_count();
        String strNewCount;
        strNewCount = newCount >= 999 ? "999+" : String.valueOf(newCount);
        if (0 == newCount) {
            viewHolder.newGroupPosts.setVisibility(View.INVISIBLE);
        } else if (newCount < 10) {
            viewHolder.newGroupPosts.setVisibility(View.VISIBLE);
            viewHolder.newGroupPosts.setText(" " + strNewCount + " ");
        } else {
            viewHolder.newGroupPosts.setVisibility(View.VISIBLE);
            viewHolder.newGroupPosts.setText(strNewCount);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                simpleTeam.setNew_count(0);
                notifyDataSetChanged();
                gotoUtil.gotoAct(context, NewTeamDetailActivity.class, simpleTeam.getGid(), simpleTeam.getName(), String.valueOf(position), NewTeamDetailActivity.TEAMDETAIL_CREATE);
            }
        });
        return convertView;
    }

    public void setdata(List<SimpleTeam> simpleTeams) {
        data = simpleTeams;
    }

    static class ViewHolder {
        TextView groupTitle;
        TextView focuseNum;
        TextView itemsNum;
        TextView newGroupPosts;
        TextView time;

        public ViewHolder(View convertView) {
            groupTitle = (TextView) convertView.findViewById(R.id.group_new_item_title);
            focuseNum = (TextView) convertView.findViewById(R.id.num_area_focus);
            itemsNum = (TextView) convertView.findViewById(R.id.num_area_items);
            newGroupPosts = (TextView) convertView.findViewById(R.id.group_new_postnum);
            time = (TextView) convertView.findViewById(R.id.group_new_item_time);
        }
    }
}
